package com.lemonsama.lesson2;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Calculator extends Frame {
    TextField textField1;
    TextField textField2;
    TextField textField3;

    public void loadFrame() {
        textField1 = new TextField(10);
        textField2 = new TextField(10);
        textField3 = new TextField(20);

        Button button = new Button("=");

        Label label = new Label("+");

        this.setLayout(new FlowLayout());
        this.add(textField1);
        this.add(label);
        this.add(textField2);
        this.add(button);
        this.add(textField3);
        pack();
        setVisible(true);

        button.addActionListener(new CalculatorListener());
        closeWindow(this);
    }

    private class CalculatorListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            int a = Integer.parseInt(textField1.getText());
            int b = Integer.parseInt(textField2.getText());
            textField3.setText((a + b) + "");
            textField1.setText("");
            textField2.setText("");
        }
    }

    public static void closeWindow(Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
